package tiepbm.app.store.adapter;

import java.util.ArrayList;
import java.util.Vector;

import tiepbm.app.store.Global;
import tiepbm.app.store.activity.R;
import tiepbm.app.store.object.Obj_App;
import tiepbm.app.util.lazyLoader.ImageLoader_nghiank;
import tiepbm.app.util.lazyLoader.MyItem_img_nghiank;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class CategoryAdapter extends ArrayAdapter<Obj_App> {

	private Activity context;
	private ArrayList<Obj_App> items = new ArrayList<Obj_App>();

	public CategoryAdapter(Context context, ArrayList<Obj_App> items) {
		super(context, R.layout.listview_item_category, items);
		this.context = (Activity) context;
		this.items = items;
	}

	/**
	 * getView function when use TouchListView must declare same this :)
	 */

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// ViewHolder holder = null;
		View v = null;
		 MyItem_img_nghiank mItem_img3 = null;
		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			v = inflater.inflate(R.layout.listview_item_category, null);

			ViewHolder holder = new ViewHolder();

			holder.imgIcon = (ImageView) v.findViewById(R.id.imageView1);
			holder.lblName = (TextView) v.findViewById(R.id.tvTitle);
			holder.lblDeveloper = (TextView) v.findViewById(R.id.tvDeveloper);
			holder.rbRate = (RatingBar) v.findViewById(R.id.rbRate);
			holder.lblMoney = (TextView) v.findViewById(R.id.tvMoney);
			v.setTag(holder);
		} else {
			v = convertView;
		}
		ViewHolder holder = (ViewHolder) v.getTag();
		holder.lblName.setText(items.get(position).getName());
		holder.lblDeveloper.setText(items.get(position).getDeveloper());
		ImageView image = holder.imgIcon;
		holder.imgIcon.setImageDrawable(getContext().getResources().getDrawable(
				R.drawable.stub));
		if(items.get(position).getTypeapp()==0){
			holder.lblMoney.setText(R.string.mienphi);
		}else{
			holder.lblMoney.setText(items.get(position).getMoney());
		}
		if(items.get(position).getRate()>0)
			holder.rbRate.setRating(items.get(position).getRate());
		
		mItem_img3 = new MyItem_img_nghiank(context, image);
		try {
			ImageLoader_nghiank.getInstance().displayImage(
					items.get(position).getIcon(), mItem_img3);
			
		} catch (OutOfMemoryError e) {
			Global.getInstance().clearCache();
		}
		return v;
	}

	static class ViewHolder {
		private ImageView imgIcon;
		private TextView lblDeveloper, lblName,lblMoney;
		private RatingBar rbRate;
		
	}
}
